package com.lpinfo.spb.plateform.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lpinfo.spb.plateform.system.dto.SysUserDto;
import com.lpinfo.spb.plateform.system.entity.SysUser;
import com.lpinfo.spb.plateform.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Slf4j
@Transactional
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements  SysUserService{

    @Override
    public IPage<SysUser> page(Page<SysUser> page, SysUserDto params) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>();
        if (!StringUtils.isEmpty(params.getUserName())&&!StringUtils.isEmpty(params.getUserPhone())){
            queryWrapper.and(wrapper -> wrapper.like(SysUser::getUserName, params.getUserName()).or().like(SysUser::getUserPhone, params.getUserPhone()));
        }
        //创建人
        if (!StringUtils.isEmpty(params.getCreateUser())){
            queryWrapper.and(wrapper -> wrapper.eq(SysUser::getCreateUser, params.getCreateUser()));
        }
        //日期区间
        if(!StringUtils.isEmpty(params.getStartTime())&&!StringUtils.isEmpty(params.getEndTime())){
            queryWrapper.between(SysUser::getCreateTime,params.getStartTime(),params.getEndTime());
        }
        ////根据不同的排序条件进行排序
        if(StringUtils.isEmpty(params.getSortField())){
            queryWrapper.orderByDesc(SysUser::getCreateTime);
        }else{
            //当request字段登录啥时 orderby 啥
            queryWrapper.orderByDesc(SysUser::getCreateTime);
        }
        IPage<SysUser> result= this.baseMapper.selectPage(page, queryWrapper);
        return result;
    }

    @Override
    public SysUser selectUserByUserName(String userName) {
        SysUser entity = new SysUser();
        entity.setUserName(userName);
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName,userName);
        return this.baseMapper.selectOne(queryWrapper);
    }


}
